//
//  JKConst.swift
//  ZhiHuDaily-Swift
//
//  Created by albert on 2018/3/2.
//  Copyright © 2018年 安永博. All rights reserved.
//

import UIKit

// MARK: - app内常量

/** 首页topStory列表key*/
public let JKTopStoryListKey = "JKTopStoryListKey"

/** 首页数据列表key 对应指定日期的story模型列表 */
public let JKStoryListModelKey = "JKStoryListModelKey"

/** 首页数据列表key 对应指定日期的story字典列表 */
public let JKStoryListDictKey = "JKStoryListDictKey"

/** 首页数据日期key 对应story列表日期 */
public let JKStoryDateKey = "JKStoryDateKey"

/** 首页数据日期key 对应story列表日期 格式化的日期 */
public let JKStoryFmtDateKey = "JKStoryFmtDateKey"

/** sectionHeader的高度 44 */
public let JKSectionHeaderH: CGFloat = 40

// MARK: - 通知name及key

/** 当前皮肤模式key */
public let JKCurrentSkinModelKey = "JKCurrentSkinModelKey"

/** 日间皮肤模式value */
public let JKDaySkinModelValue = "JKDaySkinModelValue"

/** 夜间皮肤模式value */
public let JKNightSkinModelValue = "JKNightSkinModelValue"

/** 皮肤改变的通知 */
public let JKSkinModelDidChangedNotification = "JKSkinModelDidChangedNotification"

/** 记录摇一摇夜间模式是否开启的key */
public let JKIsShakeCanChangeSkinKey = "JKIsShakeCanChangeSkinKey"

/** 记录关闭阅读位置提醒 是否开启的key */
public let JKReadPositionNoticeOffKey = "JKReadPositionNoticeOffKey"

/** 记录关闭自动离线下载 是否开启的key */
public let JKAutoDownloadKey = "JKAutoDownloadKey"

/** 摇一摇的通知 */
public let JKDeviceDidShakeNotification = "JKDeviceDidShakeNotification"

/** 用户名 */
public let JKUserNameKey = "JKUserNameKey"

/** 记录是否登录 */
public let JKIsLoginKey = "JKIsLoginKey"

/** 该项目git地址 */
public let JKMyGitUrl = "http://git.oschina.net/albertcoder/ZhiHuDaily"

/** 微信AppKey */
public let JKWXAppkey = "wxf5bb035f7f3b91bf"

/** 微博appKey */
public let JKWeiboAppKey = "3062708516"

/** 友盟统计Appkey */
public let JKUMAnalyticsAppkey = "5a7e6ad08f4a9d65dc000096"

/** 启动图片的图片来源 */
public let JKLaunchImageSourceKey = "JKLaunchImageSourceKey"

/** 启动图片的图片url */
public let JKLaunchImageUrlKey = "JKLaunchImageUrlKey"

/** 上一次启动app的日期 */
public let JKLastDateToLaunchKey = "JKLastDateToLaunchKey"

/** 已读文章缓存key */
public let JKAlreadyReadDictKey = "JKAlreadyReadDictKey"

/** 最后一次阅读的文章的索引通知 */
public let JKLastReadIndexNotification = "JKLastReadNotification"

/** 最后一次阅读的文章的索引key */
public let JKLastReadIndexKey = "JKLastReadIndexKey"



// MARK: - 之前宏定义及常量


// 屏幕尺寸
public let JKScreenBounds =  UIScreen.main.bounds
public let JKScreenH      =  UIScreen.main.bounds.height
public let JKScreenW      =  UIScreen.main.bounds.width

public let JKCellTitleFont = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)//.boldSystemFont(ofSize: 16)

// 快速设置颜色
public func JKColor(_ R: CGFloat, _ G: CGFloat, _ B: CGFloat) -> UIColor {
    
    return JKColorAlpha(R: R, G: G, B: B, A: 1)
}

public func JKColorAlpha(R: CGFloat, G: CGFloat, B: CGFloat, A:CGFloat) -> UIColor {
    
    return UIColor(red: (R)/255.0, green: (G)/255.0, blue: (B)/255.0, alpha: A)
}

// RGB相等颜色
public func JKSameRGBColor(_ RGB: CGFloat) -> UIColor {
    
    return JKSameRGBColoraAlpha(RGB: RGB, A: 1)
}

public func JKSameRGBColoraAlpha(RGB: CGFloat, A: CGFloat) -> UIColor {
    
    return UIColor(red: (RGB)/255.0, green: (RGB)/255.0, blue: (RGB)/255.0, alpha: A)
}


// 随机色
public func JKRandomColor() -> UIColor {
    
    return JKRandomColorAlpha(A: 1)
}

public func JKRandomColorAlpha(A: CGFloat) -> UIColor{
    
    return JKColorAlpha(R: CGFloat(arc4random_uniform(256)), G: CGFloat(arc4random_uniform(256)), B: CGFloat(arc4random_uniform(256)), A:A)
}



public let JKGlobalBgColor = JKColor(245, 245, 245)

public let JKNightSkinBgColor = JKColor(46, 46, 46)

public let JKNightSkinNavColor = JKSameRGBColor(60)

//public let JKMainColor = JKColor(r: 0, g: 132, b: 208)
public let JKMainColor = JKColor(38, 98, 78)

public let JKTagBg = JKColor(97, 173, 232)


public let JKTagFont = UIFont.systemFont(ofSize: 14)

public let JKCssDirectory = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent("storyDetailCss")

public let JKCssDirectoryName = "JKCssDirectory"


// MARK:- 适配iPhone X

public let JKStatusBarHeight: CGFloat = (UIApplication.shared.statusBarFrame.size.height)

public let JKIsIphoneX: Bool = {
    
    if #available(iOS 11.0, *) {
        
        return UIApplication.shared.delegate!.window!!.safeAreaInsets.bottom > 0.0
        
    }
        
    return false
}()

public let JKNavBarHeight: CGFloat = {JKIsIphoneX ? 88 : 64}()

public let JKTabBarHeight: CGFloat = {JKIsIphoneX ? 83 : 49}()

public let JKBarHeight: CGFloat = 44

/** iPhone X HomeIndicator默认高度 34 */
public let JKHomeIndicatorHeight: CGFloat = 34

/** 当前HomeIndicator高度 是iPhone X为34 否则为0 */
public let JKCurrentHomeIndicatorHeight = JKIsIphoneX ? JKHomeIndicatorHeight : 0

/** 强制退出键盘 */
public func JKHideKeyBoard() {
    
    UIApplication.shared.delegate?.window!?.endEditing(true)
}

// 判断是否包含中文
public func jk_containChinese(str: String?) -> Bool {
    
    if str == nil {
        
        return false
    }
    
    for (_, value) in str!.enumerated() {
        
        // UCS中码位为0x4E00~0x9FFF的码位,就用于表示“中日韩统一表意文字
        
        if ("\u{4E00}" <= value  && value <= "\u{9FFF}") {
            
            return true
        }
    }
    
    return false
}

extension DispatchQueue {
    
    private static var _onceTracker = [String]()
    
    public class func once(token: String, block: () -> ()) {
        objc_sync_enter(self)
        defer {
            objc_sync_exit(self)
        }
        if _onceTracker.contains(token) {
            return
        }
        _onceTracker.append(token)
        block()
    }
    
    func async(block: @escaping ()->()) {
        
        self.async(execute: block)
    }
    
    func afterSeconds(time: Int, block: @escaping ()->()) {
        
        let afterTime = DispatchTime.now() + DispatchTimeInterval.seconds(time)
        
        self.asyncAfter(deadline: afterTime, execute: block)
    }
    
    func afterMilliseconds(time: Int, block: @escaping ()->()) {
        
        let afterTime = DispatchTime.now() + DispatchTimeInterval.milliseconds(time)
        
        self.asyncAfter(deadline: afterTime, execute: block)
    }
}

extension String {
    
    public func substring(from index: Int) -> String {
        
        if self.count > index {
            
            let startIndex = self.index(self.startIndex, offsetBy: index)
            let subString = self[startIndex..<self.endIndex]
            
            return String(subString)
            
        } else {
            
            return self
        }
    }
}
